//实现单链表
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
}Node,*LinkedList;

//创建链表
void CreateList(LinkedList *L) {
    *L = (Node*)malloc(sizeof(Node));
    (*L)->next = NULL;
}
//增删改
void Add(LinkedList L, int data) {
    Node* p = (Node*)malloc(sizeof(Node));
    p->data = data;
    p->next = NULL;
    if (L->next == NULL) {
        L->next = p;
    } else {
        Node* q = L;
        while (q->next != NULL) {
            q = q->next;
        }
        q->next = p;
    }
}
//删除
void Delete(LinkedList L, int data) {
    Node* p = L;
    while (p->next != NULL && p->next->data != data) {
        p = p->next;
    }
    if (p->next != NULL) {
        Node* q = p->next;
        p->next = q->next;
        free(q);
    } else {
        printf("未找到该元素\n");
    }
}
//修改
void Modify(LinkedList L, int oldData, int newData) {
    Node* p = L;
    while (p->next != NULL && p->next->data != oldData) {
        p = p->next;
    }
    if (p->next != NULL) {
        p->next->data = newData;
    } else {
        printf("未找到该元素\n");
    }
}
//查找
void Search(LinkedList L, int data) {
    Node* p = L->next;
    while (p != NULL && p->data != data) {
        p = p->next;
    }
    if (p != NULL) {
        printf("找到元素\n");
    } else {
        printf("未找到该元素\n");
    }
}
//打印链表
void PrintList(LinkedList L) {
    Node* p = L->next;
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

int main (){
    LinkedList L;
    CreateList(&L);
    Add(L, 1);
    Add(L, 2);
    Add(L, 3);
    PrintList(L);
    Delete(L, 2);
    Modify(L, 3, 4);
    Search(L, 4);
    PrintList(L);
    return 0;
}